#include <func.h>

int main(int argc,char **argv)
{
    //传入参数： ip地址，端口号
    ARGS_CHECK(argc,3);
    //初始化socket，带有一个缓冲区
    int socketFd = socket(AF_INET,SOCK_STREAM,0);//AF_INET代表ipv4 SOCK_STREAM代表流式socket，
                                                //用于tcp协议，0表示i
    ERROR_CHECK(socketFd,-1,"socket");
    printf("client: socketFd:%d\n",socketFd);
    struct sockaddr_in sockad;
    bzero(&sockad,sizeof(sockad));
    sockad.sin_family = AF_INET;
    sockad.sin_addr.s_addr = inet_addr(argv[1]);//inet_addr:把点分10进制的ip转换成in_addr_t(32位unsinged int)
    sockad.sin_port = htons(atoi(argv[2]));
    
    int ret = connect(socketFd,(struct sockaddr*)&sockad,sizeof(sockad));
    ERROR_CHECK(ret,-1,"connect");
    char str[128];
    printf("input your words to server:\n");
    read(STDIN_FILENO,str,sizeof(str));

    send(socketFd,str,strlen(str)-1,0);//-1 剪掉回车

    bzero(str,sizeof(str));
    recv(socketFd,str,sizeof(str)-1,0);
    printf("client: received: %s\n",str);
    return 0;
}

